﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Pathfinding
{
    public class WayPointRoot : MonoBehaviour
    {
        public float WayObjectScale = 1f;
        public List<WayPointObject> WayPoints = new List<WayPointObject>();
        public void LoadFromConfig(Hashtable configData) 
        {
          object[] PointObjs = (object[])configData[":points"];
          Hashtable LinkDic = (Hashtable)configData[":links"];

          for (int i = 0; i < PointObjs.Length;i++ ) 
          {
            object[] curPointObj = (object[])PointObjs[i];
            WayPointObject NewWayPointG = NewWayPoint();
            NewWayPointG.ID = (int)curPointObj[0];
            NewWayPointG.PointRoot = this;
            NewWayPointG.Init(NewWayPointG.ID, (float)curPointObj[1],(float)curPointObj[1]);
            NewWayPointG.transform.localPosition = new Vector3((float)curPointObj[1],(float)curPointObj[2],0);
            object[] links = (object[])curPointObj[3];
            NewWayPointG.DstPoints = new int[links.Length];
            for (int j = 0; j < links.Length;j++) 
            {  
                object[] linkobj = (object[])LinkDic[(string)links[j]];
                int endPoint = (int)linkobj[2];
                NewWayPointG.DstPoints[j] = endPoint;
                GameObject LineObjs = (GameObject)GameObject.Instantiate( Resources.Load("WayLine"));
                WayLinkObject WaylinkObj= LineObjs.AddComponent<WayLinkObject>();
                WaylinkObj.PointRoot = this;
                WaylinkObj.transform.parent = NewWayPointG.transform.parent;
                WaylinkObj.New((int)linkobj[1], 
                               (int)linkobj[2],
                               (string)linkobj[0],
                               (WayLinkObject.WayActionType)(int)linkobj[4],
                               (WayLinkObject.WayType)((int)linkobj[3]), (WayLinkObject.WayActionType)linkobj[5]);
                NewWayPointG.Links.Add(endPoint, WaylinkObj);
                LineObjs.name = WaylinkObj.StartPointID+"-"+WaylinkObj.EndPointID;
            }
          }
        }

        public string Export2LispData() 
        {
            /*
             { 
               :points [ [id x y [linkid0 linkid1]] ]
                old - :links { id [id startid endid actionType wayType] }
                new - :links { id [id startid endid wayType S2DActionType D2SActionType]}
             }
            */
            Hashtable retTable = new Hashtable();
           Hashtable WayLinks = new Hashtable(); 
           object[] WayPointArr = new object[this.WayPoints.Count];
           retTable.Add(":points",WayPointArr); 
           for (int i = 0; i < WayPoints.Count;i++)
           {
               WayPointObject CurPointG = this.WayPoints[i];
               object[] PointDataArr = new object[4];
               PointDataArr[0] = CurPointG.ID;
               PointDataArr[1] = CurPointG.transform.position.x;
               PointDataArr[2] = CurPointG.transform.position.y;
               PointDataArr[3] = new object[CurPointG.Links.Count];
               int j = 0;
               foreach(KeyValuePair<int,WayLinkObject> LinkItem in CurPointG.Links)
               {
                   ((object[])PointDataArr[3])[j] = LinkItem.Value.linkID; 
                   object[] newLinkArr= new object[6];
                   newLinkArr[0] = LinkItem.Value.linkID;
                   newLinkArr[1] = LinkItem.Value.StartPointID;
                   newLinkArr[2] = LinkItem.Value.EndPointID;
                   newLinkArr[3] = (int)LinkItem.Value.LinkWayType;
                   newLinkArr[4] = (int)LinkItem.Value.S2DActionType;
                   newLinkArr[5] = (int)LinkItem.Value.D2SActionType;
                   WayLinks.Add(LinkItem.Value.linkID,newLinkArr);
                   j++;
               }
               WayPointArr[i] = PointDataArr;
           }
           retTable.Add(":links",WayLinks);
           return SGLisp.Helper.DataToLispString(retTable);
        }

        public WayPointObject NewWayPoint() 
        {
            GameObject newWayPointG = (GameObject)GameObject.Instantiate(Resources.Load("WayPoint"));
            newWayPointG.transform.parent = transform;
            WayPointObject newWayPoint = newWayPointG.AddComponent<WayPointObject>();
            newWayPoint.PointRoot = this;
            newWayPoint.Init(WayPoints.Count);
            this.WayPoints.Add(newWayPoint);
            return newWayPoint;
        }
     

        public WayPointObject GetWayPointById(int id) 
        {
            for (int i = 0; i < this.WayPoints.Count;i++) 
            {
                if (this.WayPoints[i].ID == id) 
                {
                  return this.WayPoints[i];
                }
            }
            return null;
        }

        public void ClearWayPoint() 
        {
            this.WayPoints.Clear();
            while(transform.childCount>0) 
            {
                DestroyImmediate(transform.GetChild(0).gameObject);
            }
        }

        public void LateUpdate()
        {
            this.checkLinks();
        }

        public void Update() 
        {
           
        }

        public void checkLinks() 
        {
            for (int i = 0; i < this.WayPoints.Count;i++ )
            { 
              WayPointObject WayObj =  this.WayPoints[i];
              WayObj.CheckGenLink();
            } 
        }
         
    }
}
